import { Injectable } from '@angular/core';
import { BaseHttpService } from '../../shared/services/baseHttp.service';
import { StorageService } from '../../shared/services/storage.service';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { InsightModel } from './models/insight.model';
import { NewsBaseModel } from './models/news.model';
import { EventListWrapper, EventModel } from './models/event.model';
import * as moment from 'moment';

/**
 * Home main service
 *
 * Author: Sorin Spiru, Andrei Istrate
 * Date: 13.01.2018
 */
@Injectable()
export class KnowledgeCenterService extends BaseHttpService {

    constructor(public http: HttpClient) {
        super();
    }

    /**
     * Get spotlight data
     */
    public getSpotlight(showFive: boolean): Observable<NewsBaseModel[]> {
        const userInfo = StorageService.get().userInfo;

        const listOptions: any = {
            skip: 0,
            take: showFive ? 5 : 10,
            orderBy: ['DateCreated'],
            orderAscending: true,
            includeAllFields: true
        };

        let params: HttpParams = new HttpParams();
        params = params.set('term', '')
            .set('category', '')
            .set('accountId', userInfo.accountId)
            .set('isNameEqual', 'false');
        params = this.urlEncodeObject(listOptions, params);

        return this.http.get<NewsBaseModel[]>(this.serverUrl + 'newsserv/api/v1/news', { params: params });
    }

    /**
     * Get events
     */
    public getEvents(showFive: boolean, includeAllFields = false): Observable<EventListWrapper> {
        const listOptions: any = { orderBy: ['StartDate'] };

        let params: HttpParams = new HttpParams();
        params = params.set('term', '')
            .set('eventType', '')
            .set('skip', '0')
            .set('take', showFive ? '5' : '10')
            .set('includeAllFields', includeAllFields.toString())
            .set('getCount', 'false')
            .set('orderAscending', 'false')
            .set('isPublished', 'true');
        params = this.urlEncodeObject(listOptions, params);

        return this.http.get<EventListWrapper>(this.serverUrl + 'eventsserv/api/v1/events', { params: params });
    }

    /**
     * Get events
     */
    public getEventById(eventId): Observable<EventModel> {
        let params: HttpParams = new HttpParams();
        params = params.set('includeAllFields', 'true');
        params = this.urlEncodeObject(params);

        return this.http.get<EventModel>(this.serverUrl + 'eventsserv/api/v1/events/' + eventId, { params: params });
    }

    /**
     * Get knowledge center data
     */
    public getKnowledge(showFive: boolean, includeAllFields = false): Observable<InsightModel[]> {
        const options: any = {
            skip: 0,
            take: showFive ? 5 : 0,
            orderBy: ['DatePublished'],
            orderAscending: false,
            includeAllFields: includeAllFields
        };

        const filter = {
            status: 1,
            noLongerThan: moment().subtract(4, 'years').format('YYYY-MM-DD')
        };

        let params = this.urlEncodeObject(options);
        params = this.urlEncodeObject(filter, params);
        return this.http.get<InsightModel[]>(this.serverUrl + 'insightsserv/api/v1/insights', { params: params });
    }

    /**
     * Get knowledge center data
     */
    public getKnowledgeById(insightId): Observable<InsightModel> {
        const options = {
            includeAllFields: true
        };

        const filter = {
            status: 1
        };

        let params = this.urlEncodeObject(options);
        params = this.urlEncodeObject(filter, params);

        return this.http.get<InsightModel>(this.serverUrl + 'insightsserv/api/v1/insights/' + insightId, { params: params });
    }
}
